<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/contrastive_loss_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - contrastive_loss_layer.cpp<span style="font-size: 80%;"> (source / <a href="contrastive_loss_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">63</td>
            <td class="headerCovTableEntryLo">3.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">14</td>
            <td class="headerCovTableEntryLo">14.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;vector&gt;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &quot;caffe/layers/contrastive_loss_layer.hpp&quot;
<span class="lineNum">       5 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : namespace caffe {
<a name="8"><span class="lineNum">       8 </span>            : </a>
<span class="lineNum">       9 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      10 </span><span class="lineNoCov">          0 : void ContrastiveLossLayer&lt;Dtype&gt;::LayerSetUp(</span>
<span class="lineNum">      11 </span>            :   const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      12 </span><span class="lineNoCov">          0 :   LossLayer&lt;Dtype&gt;::LayerSetUp(bottom, top);</span>
<span class="lineNum">      13 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[0]-&gt;channels(), bottom[1]-&gt;channels());</span>
<span class="lineNum">      14 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[0]-&gt;height(), 1);</span>
<span class="lineNum">      15 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[0]-&gt;width(), 1);</span>
<span class="lineNum">      16 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[1]-&gt;height(), 1);</span>
<span class="lineNum">      17 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[1]-&gt;width(), 1);</span>
<span class="lineNum">      18 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[2]-&gt;channels(), 1);</span>
<span class="lineNum">      19 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[2]-&gt;height(), 1);</span>
<span class="lineNum">      20 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[2]-&gt;width(), 1);</span>
<span class="lineNum">      21 </span><span class="lineNoCov">          0 :   diff_.Reshape(bottom[0]-&gt;num(), bottom[0]-&gt;channels(), 1, 1);</span>
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :   diff_sq_.Reshape(bottom[0]-&gt;num(), bottom[0]-&gt;channels(), 1, 1);</span>
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :   dist_sq_.Reshape(bottom[0]-&gt;num(), 1, 1, 1);</span>
<span class="lineNum">      24 </span>            :   // vector of ones used to sum along channels
<span class="lineNum">      25 </span><span class="lineNoCov">          0 :   summer_vec_.Reshape(bottom[0]-&gt;channels(), 1, 1, 1);</span>
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom[0]-&gt;channels(); ++i)</span>
<span class="lineNum">      27 </span><span class="lineNoCov">          0 :     summer_vec_.mutable_cpu_data()[i] = Dtype(1);</span>
<span class="lineNum">      28 </span><span class="lineNoCov">          0 : }</span>
<a name="29"><span class="lineNum">      29 </span>            : </a>
<span class="lineNum">      30 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      31 </span><span class="lineNoCov">          0 : void ContrastiveLossLayer&lt;Dtype&gt;::Forward_cpu(</span>
<span class="lineNum">      32 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,
<span class="lineNum">      33 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      34 </span><span class="lineNoCov">          0 :   int count = bottom[0]-&gt;count();</span>
<span class="lineNum">      35 </span><span class="lineNoCov">          0 :   caffe_sub(</span>
<span class="lineNum">      36 </span>            :       count,
<span class="lineNum">      37 </span>            :       bottom[0]-&gt;cpu_data(),  // a
<span class="lineNum">      38 </span>            :       bottom[1]-&gt;cpu_data(),  // b
<span class="lineNum">      39 </span>            :       diff_.mutable_cpu_data());  // a_i-b_i
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :   const int channels = bottom[0]-&gt;channels();</span>
<span class="lineNum">      41 </span><span class="lineNoCov">          0 :   Dtype margin = this-&gt;layer_param_.contrastive_loss_param().margin();</span>
<span class="lineNum">      42 </span>            :   bool legacy_version =
<span class="lineNum">      43 </span>            :       this-&gt;layer_param_.contrastive_loss_param().legacy_version();
<span class="lineNum">      44 </span>            :   Dtype loss(0.0);
<span class="lineNum">      45 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom[0]-&gt;num(); ++i) {</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :     dist_sq_.mutable_cpu_data()[i] = caffe_cpu_dot(channels,</span>
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :         diff_.cpu_data() + (i*channels), diff_.cpu_data() + (i*channels));</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :     if (static_cast&lt;int&gt;(bottom[2]-&gt;cpu_data()[i])) {  // similar pairs</span>
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :       loss += dist_sq_.cpu_data()[i];</span>
<span class="lineNum">      50 </span>            :     } else {  // dissimilar pairs
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :       if (legacy_version) {</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :         loss += std::max(margin - dist_sq_.cpu_data()[i], Dtype(0.0));</span>
<span class="lineNum">      53 </span>            :       } else {
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :         Dtype dist = std::max&lt;Dtype&gt;(margin - sqrt(dist_sq_.cpu_data()[i]),</span>
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :           Dtype(0.0));</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :         loss += dist*dist;</span>
<span class="lineNum">      57 </span>            :       }
<span class="lineNum">      58 </span>            :     }
<span class="lineNum">      59 </span>            :   }
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :   loss = loss / static_cast&lt;Dtype&gt;(bottom[0]-&gt;num()) / Dtype(2);</span>
<span class="lineNum">      61 </span><span class="lineNoCov">          0 :   top[0]-&gt;mutable_cpu_data()[0] = loss;</span>
<span class="lineNum">      62 </span><span class="lineNoCov">          0 : }</span>
<a name="63"><span class="lineNum">      63 </span>            : </a>
<span class="lineNum">      64 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      65 </span><span class="lineNoCov">          0 : void ContrastiveLossLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">      66 </span>            :     const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :   Dtype margin = this-&gt;layer_param_.contrastive_loss_param().margin();</span>
<span class="lineNum">      68 </span>            :   bool legacy_version =
<span class="lineNum">      69 </span>            :       this-&gt;layer_param_.contrastive_loss_param().legacy_version();
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; 2; ++i) {</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :     if (propagate_down[i]) {</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :       const Dtype sign = (i == 0) ? 1 : -1;</span>
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :       const Dtype alpha = sign * top[0]-&gt;cpu_diff()[0] /</span>
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :           static_cast&lt;Dtype&gt;(bottom[i]-&gt;num());</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :       int num = bottom[i]-&gt;num();</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :       int channels = bottom[i]-&gt;channels();</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :       for (int j = 0; j &lt; num; ++j) {</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :         Dtype* bout = bottom[i]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">      79 </span><span class="lineNoCov">          0 :         if (static_cast&lt;int&gt;(bottom[2]-&gt;cpu_data()[j])) {  // similar pairs</span>
<span class="lineNum">      80 </span><span class="lineNoCov">          0 :           caffe_cpu_axpby(</span>
<span class="lineNum">      81 </span>            :               channels,
<span class="lineNum">      82 </span>            :               alpha,
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :               diff_.cpu_data() + (j*channels),</span>
<span class="lineNum">      84 </span>            :               Dtype(0.0),
<span class="lineNum">      85 </span>            :               bout + (j*channels));
<span class="lineNum">      86 </span>            :         } else {  // dissimilar pairs
<span class="lineNum">      87 </span>            :           Dtype mdist(0.0);
<span class="lineNum">      88 </span>            :           Dtype beta(0.0);
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :           if (legacy_version) {</span>
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :             mdist = margin - dist_sq_.cpu_data()[j];</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :             beta = -alpha;</span>
<span class="lineNum">      92 </span>            :           } else {
<span class="lineNum">      93 </span><span class="lineNoCov">          0 :             Dtype dist = sqrt(dist_sq_.cpu_data()[j]);</span>
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :             mdist = margin - dist;</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :             beta = -alpha * mdist / (dist + Dtype(1e-4));</span>
<span class="lineNum">      96 </span>            :           }
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :           if (mdist &gt; Dtype(0.0)) {</span>
<span class="lineNum">      98 </span><span class="lineNoCov">          0 :             caffe_cpu_axpby(</span>
<span class="lineNum">      99 </span>            :                 channels,
<span class="lineNum">     100 </span>            :                 beta,
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :                 diff_.cpu_data() + (j*channels),</span>
<span class="lineNum">     102 </span>            :                 Dtype(0.0),
<span class="lineNum">     103 </span>            :                 bout + (j*channels));
<span class="lineNum">     104 </span>            :           } else {
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :             caffe_set(channels, Dtype(0), bout + (j*channels));</span>
<span class="lineNum">     106 </span>            :           }
<span class="lineNum">     107 </span>            :         }
<span class="lineNum">     108 </span>            :       }
<span class="lineNum">     109 </span>            :     }
<span class="lineNum">     110 </span>            :   }
<span class="lineNum">     111 </span><span class="lineNoCov">          0 : }</span>
<a name="112"><span class="lineNum">     112 </span>            : </a>
<span class="lineNum">     113 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     114 </span><span class="lineNoCov">          0 : STUB_GPU(ContrastiveLossLayer);</span>
<span class="lineNum">     115 </span>            : #endif
<a name="116"><span class="lineNum">     116 </span>            : </a>
<span class="lineNum">     117 </span>            : INSTANTIATE_CLASS(ContrastiveLossLayer);
<a name="118"><span class="lineNum">     118 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(ContrastiveLoss);</span></a>
<span class="lineNum">     119 </span>            : 
<span class="lineNum">     120 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
